<?php
/*
 *
 *      install.php - v1.0.0 @07.07.2009 22:29:40 CEST
 *
 *      StudioGallery Copyright (C) 2009 Rafal Kusior (spuavick) <spuavick@interia.pl>
 *
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *      -----------------------------------------------------------------------------
 *
 *      Niniejszy program jest wolnym oprogramowaniem - możesz go rozpowszechniać
 *      dalej i/lub modyfikować na warunkach Powszechnej Licencji Publicznej GNU
 *      wydanej przez Fundację Wolnego Oprogramowania, według wersji 2 tej Licencji
 *      lub dowolnej z późniejszych wersji.
 *
 *      Niniejszy program rozpowszechniany jest z nadzieją, iż będzie on użyteczny -
 *      jednak BEZ ŻADNEJ GWARANCJI, nawet domyślnej gwarancji PRZYDATNOŚCI HANDLOWEJ
 *      albo PRZYDATNOŚCI DO OKREŚLONYCH ZASTOSOWAŃ. Bliższe informacje na ten temat
 *      można uzyskać z Powszechnej Licencji Publicznej GNU.
 *
 *      Kopia Powszechnej Licencji Publicznej GNU powinna zostać ci dostarczona
 *      razem z tym programem. Jeżeli nie została dostarczona, odwiedź
 *      <http://www.gnu.org/licenses/>
 */

$preU = "";
require_once($preU.'sg-include/sg_db.php');
require_once($preU.'sg-include/version.php');
require_once($preU.'sg-include/functions.php');

if ( getinstall('install') != true ) {
?>
	<html>
	<head>
		<title>StudioGallery <?=version?> - <?=_("instalacja")?></title>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
		<link rel="stylesheet" type="text/css" href="sg-admin/css/style.css" />
		<meta name="generator" content="StudioGallery <?=version?>" />
	</head>
	<body>
	<div class="main">
		<div class="header"><?= _("Instalacja StudioGallery")?> <?=version?></div>
		<div class="content">
			<h1><?= _("Proces instalacji StudioGallery")?> <?version?></h1>
			<?php
   				if (!$_POST || $_POST['title'] == "" || $_POST['url'] == "") {
			?>
				<form name="install" id="install" method="post" action="?step=next">
				<div class="form">
					<div class="l">
						<label name="mail" for="mail"><?= _("Twój adres E-mail")?>:</label>
						<input type="text" name="mail" id="mail" value="" />
					</div>
					<div class="clr"></div>
				</div>
				<div class="form">
					<div class="l">
						<label name="title" for="title"><?= _("Tytuł strony")?>:</label>
						<input type="text" name="title" id="title" value="StudioGallery" />
					</div>
					<div class="clr"></div>
				</div>
				<div class="form">
					<div class="l">
						<label name="page" for="url"><?= _("Adres strony")?>:</label>
						<input type="text" name="url" id="url" value="http://<?=urlPage()?>" />
					</div>
					<div class="clr"></div>
				</div>
				<div class="form">
					<div class="l">
						<label name="pre" for="pre"><?= _("Prefix bazy danych")?>:</label>
						<input type="text" name="pre" id="pre" value="sg_" />
					</div>
					<div class="clr"></div>
				</div>
				<?php

					$error = array();
					if ( checkPermission("./sg-include/install.php")) {

				?>
				<div class="form">
					<div class="c">
						<?=_("Plik")?> ./sg-include/install.php <span class="ok"><?=_("możliwość zapisu")?></span>
					</div>
				</div>
				<?php

					} else {

						$error[] = 1;
				?>
				<div class="form">
					<div class="c">
						<?=_("Plik")?> ./sg-include/install.php <span class="error"><?=_("brak możliwości zapisu")?></span>
					</div>
				</div>
				<?php

					}

					if ( checkPermission("./photos")) {

				?>
				<div class="form">
					<div class="c">
						<?=_("Katalog")?> ./photos <span class="ok"><?=_("możliwość zapisu")?></span>
					</div>
				</div>
				<?php

					} else {

						$error[] = 1;

				?>
				<div class="form">
					<div class="c">
						<?=_("Katalog")?> ./photos <span class="error"><?=_("brak możliwości zapisu")?></span>
					</div>
				</div>
				<?php

					}

					if ( checkPermission("./templates_c")) {

				?>
				<div class="form">
					<div class="c">
						<?=_("Katalog")?> ./templates_c <span class="ok"><?=_("możliwość zapisu")?></span>
					</div>
				</div>
				<?php

					} else {

						$error[] = 1;

				?>
				<div class="form">
					<div class="c">
						<?=_("Katalog")?> ./templates_c  <span class="error"><?=_("brak możliwości zapisu")?></span>
					</div>
				</div>
				<?php

					}

					if (count($error) == 0) {

				?>
				<div class="form">
					<div class="c">
						<input type="submit" value="<?=_("dalej")?>&raquo;" class="button" />
					</div>
				</div>
				<?php

					} else {

				?>
				<div class="form">
					<div class="c">
						<?=_("Popraw błędy i odśwież stronę")?>
					</div>
				</div>
				<?php

					 }

				?>
				</form>
		<?php

			} else {

				$db = new db_class;
				$password = passGen(8);
				$pre = strip_tags(trim($_POST['pre']));
				$mail = strip_tags(trim($_POST['mail']));
					$sql = "DROP TABLE IF EXISTS `".$pre."blog`;
CREATE TABLE IF NOT EXISTS `".$pre."blog` (
  `news_id` mediumint(8) unsigned NOT NULL auto_increment,
  `news_title` varchar(255) default NULL,
  `news_text` longtext,
  `news_date` int(11) default NULL,
  `news_show` int(8) default '0',
  PRIMARY KEY  (`news_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

INSERT INTO `".$pre."blog` (`news_id`, `news_title`, `news_text`, `news_date`, `news_show`) VALUES
(1, 'Witaj Świecie', '<p>To jest przykładowy wpis, który możesz zmienić lub usunć w panelu administracyjnym, w zakładce wpisy.\n</p>', 1247124960, 1);

DROP TABLE IF EXISTS `".$pre."category`;
CREATE TABLE IF NOT EXISTS `".$pre."category` (
  `cat_id` mediumint(8) unsigned NOT NULL auto_increment,
  `cat_sub_id` int(8) default '0',
  `cat_name` varchar(255) default NULL,
  `cat_desc` mediumtext,
  `cat_show` int(8) default '1',
  PRIMARY KEY  (`cat_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

INSERT INTO `".$pre."category` (`cat_id`, `cat_sub_id`, `cat_name`, `cat_desc`, `cat_show`) VALUES
(2, 1, 'podkategoria', NULL, 1),
(1, 0, 'kategoria', NULL, 1);

DROP TABLE IF EXISTS `".$pre."comments`;
CREATE TABLE IF NOT EXISTS `".$pre."comments` (
  `com_id` mediumint(8) unsigned NOT NULL auto_increment,
  `com_photo_id` int(8) default '0',
  `com_user` varchar(40) default NULL,
  `com_email` varchar(100) default NULL,
  `com_www` varchar(100) default NULL,
  `com_desc` longtext,
  `com_date` int(11) default '0',
  `com_ip` varchar(255) character set ucs2 default '0.0.0.0',
  `com_news_id` int(8) default '0',
  `com_show` int(10) default '0',
  PRIMARY KEY  (`com_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

INSERT INTO `".$pre."comments` (`com_id`, `com_photo_id`, `com_user`, `com_email`, `com_www`, `com_desc`, `com_date`, `com_ip`, `com_news_id`, `com_show`) VALUES
(1, 0, 'ktoś', 'jakis@adres.com', '', 'to jest przykładowy komentarz, możesz go usunć w zakładce komentarze', 1247125020, '192.168.2.1', 1, 1);

DROP TABLE IF EXISTS `".$pre."configuration`;
CREATE TABLE IF NOT EXISTS `".$pre."configuration` (
  `conf_id` mediumint(8) unsigned NOT NULL auto_increment,
  `conf_url` varchar(255) default NULL,
  `conf_start_page` int(8) default '0',
  `conf_comments` int(8) default '2',
  `conf_admin` varchar(255) default NULL,
  `conf_pass` varchar(255) default NULL,
  `conf_exif` int(8) default '1',
  `conf_version` varchar(20) default NULL,
  `conf_watermark` int(8) default '0',
  `conf_sign_watermark` varchar(255) default NULL,
  `conf_votes` int(8) default '1',
  `conf_show` int(8) default '1',
  `conf_desc` mediumtext,
  `conf_keywords` mediumtext,
  `conf_page` int(8) default '1',
  `conf_blog` int(8) default '1',
  `conf_key` varchar(255) NOT NULL default '0',
  `conf_title` varchar(255) NOT NULL,
  `conf_theme` varchar(255) default 'studioGallery',
  `conf_photo_small` int(10) NOT NULL default '50',
  `conf_photo_min` int(10) NOT NULL default '150',
  `conf_photo_big` int(10) NOT NULL default '1024',
  `conf_jquery_lightbox` int(10) default '1',
  `conf_page_title` varchar(255) default 'info',
  `conf_blog_title` varchar(255) default 'blog',
  `conf_news_item` int(10) default '10',
  `conf_email` varchar(100) default NULL,
  `conf_smtp_host` varchar(100) default NULL,
  `conf_smtp_pass` varchar(100) default NULL,
  `conf_mail` int(8) default '1',
  `conf_plugins` longtext,
  PRIMARY KEY  (`conf_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

DROP TABLE IF EXISTS `".$pre."page`;
CREATE TABLE IF NOT EXISTS `".$pre."page` (
  `page_id` mediumint(8) NOT NULL auto_increment,
  `page_title` varchar(255) default NULL,
  `page_desc` mediumtext,
  PRIMARY KEY  (`page_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

DROP TABLE IF EXISTS `".$pre."photo`;
CREATE TABLE IF NOT EXISTS `".$pre."photo` (
  `photo_id` mediumint(8) unsigned NOT NULL auto_increment,
  `photo_title` varchar(255) default NULL,
  `photo_desc` mediumtext,
  `photo_exif` mediumtext,
  `photo_date` int(11) default '0',
  `photo_rate` int(8) default '0',
  `photo_votes` int(8) default '0',
  `photo_category` int(8) default '0',
  `photo_show` int(10) default '0',
  `photo_file` varchar(255) default NULL,
  `photo_cat_id` int(10) default '0',
  `photo_visit` int(10) default '0',
  PRIMARY KEY (`photo_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

DROP TABLE IF EXISTS `".$pre."plugins`;
CREATE TABLE IF NOT EXISTS `".$pre."plugins` (
  `plugin_id` mediumint(8) unsigned NOT NULL auto_increment,
  `plugin_id_file` varchar(50) default NULL,
  `plugin_author` varchar(50) default NULL,
  `plugin_name` varchar(50) default NULL,
  `plugin_desc` mediumtext,
  `plugin_url` varchar(100) default NULL,
  `plugin_active` mediumint(8) NOT NULL default '0',
  `plugin_version` varchar(10) default NULL,
  `plugin_options` longtext,
  PRIMARY KEY  (`plugin_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

DROP TABLE IF EXISTS `".$pre."rating`;
CREATE TABLE IF NOT EXISTS `".$pre."rating` (
  `rating_id` mediumint(8) unsigned NOT NULL auto_increment,
  `rating_photo_id` int(10) default '0',
  `rating_rating` int(10) default '0',
  `rating_ip` varchar(255) default '0.0.0.0',
  PRIMARY KEY  (`rating_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
";

					$db->execute($sql);

					echo "<div class=\"form\"><div class=\"c\">";
					echo _("Struktura bazy danych utworzona");
					echo "</div></div>";

				$array = array(
	'menu_admin','plugin_options','plugin_info','setup_presentation','setup_1','setup_2','setup_3',
	'setup_1_save','setup_2_save','setup_3_save','post','post_save','photo','photo_delete','photo_update','photo_save',

	'header','menu','footer','content','cat_photo','sidebar','photoinfo','photos','newsitem','newslist','page','navigation','lastphoto','lastnews','pagination','cat_gallery'
);
				$data = array(
							'conf_pass' => md5($password),
							'conf_admin' => 'admin',
							'conf_title' => strip_tags(trim($_POST['title'])),
							'conf_url' => strip_tags(trim($_POST['url'])),
							'conf_keywords' => 'studioGallery, skrypt, galeria, fotografia, zdjęcia',
							'conf_desc' => 'galeria oparta o studioGallery',
							'conf_version' => version,
							'conf_theme' => 'studioGallery',
							'conf_email' => $mail,
							'conf_plugins' => serialize($array)
							);

				$configuration = $db->insert_array($pre."configuration", $data);
				$db->print_last_query();

				$install = "sg-include/install.php";
				$content = "<?php\n
define('_pre_','".$pre."');
define('install',true);\n
?>";
				$plik = fopen($install, "w+");

				if (flock($plik, LOCK_EX)) {

					fwrite($plik, $content);
					flock($plik, LOCK_UN);

				} else {

					echo _("Problem z plikiem");
					echo " <strong>./sg-include/install.php</strong> !";

				}

				fclose($plik);
				@chmod($install,0644);




		?>
				<div class="form">
					<div class="l">
						<?=_("Login")?>:
					</div>
					<div class="l">
						<strong>admin</strong>
					</div>
				</div>
				<div class="form">
					<div class="l">
						<?=_("Hasło")?>:
					</div>
					<div class="l">
					<strong><?=$password?></strong>
					</div>
				</div>
				<div class="form">
					<div class="c">
						<?=_("Skopiuj hasło, będzie konieczne przy pierwszym logowaniu!")?>
					</div>
				</div>
				<div class="form">
					<div class="c">
						<input type="button" onclick="window.location.href='./sg-admin'" class="button_big" value="przejdź do zarządzania" />
					</div>
				</div>
	<?php } ?>
			</div>
			<div class="footer">
				<?=_("Wszelkie prawa zastrzeżone")?> &copy;<?=$rok?> <a href="http://rsks.net">RSKstudio</a>
			</div>
		</div>
		</body>
	</html>
<?php

} else {

	header("Location: ./index.php");

}
function getinstall($install)
{
    return (defined($install)) ? constant($install) : null;
}
?>
